﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor.Operations
{
	internal class MergeEntities : Operation
	{
        Data.EntityRecord EntityA = null;
        Data.EntityRecord EntityB = null;
        List<Data.EntityElement> ElementsA;
        List<Data.EntityElement> ElementsB;

        int EntityAIndex;
        int EntityBIndex;

        internal MergeEntities(EditorState ES)
		{
            if (ES.MultiSelection.Count == 2)
            {
                EntityA = ES.GetTopObject(ES.MultiSelection[0]) as Data.EntityRecord;
                EntityB = ES.GetTopObject(ES.MultiSelection[1]) as Data.EntityRecord;
            }

            if (EntityA != null && EntityB != null) 
            {
                ElementsA = new List<Jemgine.Data.EntityElement>(EntityA.AttachedElements);
                ElementsB = new List<Jemgine.Data.EntityElement>(EntityB.AttachedElements);

                EntityAIndex = ES.MultiSelection[0].TopIndex;
                EntityBIndex = ES.MultiSelection[1].TopIndex;
            }
		}


		internal override ReturnCode Do(EditorState ES)
		{
            if (EntityA == null || EntityB == null)
            {
                System.Windows.Forms.MessageBox.Show("Select 2 (and only 2) entities.");
                return ReturnCode.DoNotAddToUndoStack;
            }

            foreach (var Element in ElementsB)
            {
                if (Element is Data.Translatable)
                {
                    Object tag = ( Element as Data.Translatable ).PrepareToTranslate();
                    ( Element as Data.Translatable ).ApplyTranslation(tag, EntityB.Position - EntityA.Position);
                }
            }

            EntityA.AttachedElements.Clear();
            EntityA.AttachedElements.AddRange(ElementsA);
            EntityA.AttachedElements.AddRange(ElementsB);

            ES.Map.Elements.RemoveAt(EntityBIndex);
            ES.MultiSelection.Clear();
            ES.SelectItem(Jemgine.Engine.Selection.None);
            ES._RebuildTree();
            ES.OnInvalidateMap();

			return ReturnCode.AddToUndoStack;
		}

        internal override void Undo(EditorState ES)
        {
            EntityA.AttachedElements.Clear();
            EntityA.AttachedElements.AddRange(ElementsA);

            EntityB.AttachedElements.Clear();
            EntityB.AttachedElements.AddRange(ElementsB);
            
            foreach (var Element in ElementsB)
            {
                if (Element is Data.Translatable)
                {
                    Object tag = ( Element as Data.Translatable ).PrepareToTranslate();
                    ( Element as Data.Translatable ).ApplyTranslation(tag, EntityA.Position - EntityB.Position);
                }
            }

            ES.Map.Elements.Insert(EntityBIndex, EntityB);

            ES.SelectItem(Jemgine.Engine.Selection.None);
            ES._RebuildTree();
            ES.OnInvalidateMap();
        }
	}
}
